home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / CPTUTT22.ZIP / CHAP12.TXT < prev    next >
Text File  |  1992-01-20  |  26KB  |  589 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.                                                        Chapter 12
  8.                                            FLYAWAY ADVENTURE GAME
  9.  
  10. Now that you have learned lots of things about C++, and know how
  11. to write and use a single isolated class, you have the problem of
  12. how to build a program with several classes that work together to
  13. accomplish some task.  After some amount of thought, it seems that
  14. an adventure game would be a good candidate for a relatively large
  15. example program.  It has lots of input and output and requires a
  16. good deal of flexibility while running since there are so many
  17. things that can be included in the game as obstacles, mazes, items
  18. to find, and puzzles to solve.
  19.  
  20. The adventure game presented in this chapter is unique as far as
  21. I know, since I have never heard of another adventure game
  22. featuring an airport.  The location is not nearly as important as
  23. the code used to get through the airport.  You are advised to play
  24. the game to get familiar with what the code does, then study the
  25. code to see how it works.  Finally, you are given an assignment to
  26. extend the code which will be the real test of whether you
  27. understand its operation.
  28.  
  29.  
  30.  
  31. PLAYING THE GAME
  32. _________________________________________________________________
  33.  
  34. Prior to studying the source code for this game,  ===============
  35. it would be to your advantage to spend some time    FLYAWAY.EXE
  36. playing the game to get familiar with what the    ===============
  37. game does.  Load the file FLYAWAY.EXE and begin
  38. the adventure through the airport.  The
  39. executable file is precompiled for you so you can begin executing
  40. the program before you have to compile and link the whole thing.
  41. The entire program is composed of 15 files and will take a little
  42. effort on your part to properly compile and link it, but that will
  43. come later.
  44.  
  45. If you have played adventure games before, sometimes called
  46. interactive fiction, you should begin trying various commands to
  47. find your way through the airport to your proper plane.  If you
  48. have not played before, a few hints are in order concerning how to
  49. play the game.
  50.  
  51. The object of the game is to get to your proper plane on time so
  52. you can fly away to your vacation.  Of course there a few obstacles
  53. and problems along the way and they will be brought up at the
  54. appropriate time.  It will be up to you to solve the puzzles
  55. associated with each problem.  To add a little excitement, you only
  56. have about twenty-five minutes to get to your plane, with each move
  57.  
  58.                                                         Page 12-1
  59.  
  60.                               Chapter 12 - Flyaway Adventure Game
  61.  
  62. taking a minute, so you must hurry.  Of course, just getting to the
  63. plane on time is not enough, there are a few additional
  64. requirements.  You will find what they are as you progress through
  65. the game.  You will probably find it necessary to restart the game
  66. many times before you arrive at your destination unscathed and on
  67. time.
  68.  
  69.  
  70.  
  71. THE METHOD OF PLAY
  72. _________________________________________________________________
  73.  
  74. The method of play is extremely simple.  You simply wander around
  75. the airport looking for things to do and places to go.  You move
  76. around the airport by giving the system commands to go in a
  77. direction with four choices available, north, south, east, or west.
  78. You can abbreviate any of these four direction commands to the
  79. first letter only, and you can use either upper or lower case.  The
  80. system may move you to another area of the airport, or it may tell
  81. you that you can't go that way.  Try loading the game now and
  82. typing the four directions once each to see what happens.  If this
  83. is not clear, enter the word help to get you started.
  84.  
  85. In addition to moving around, you can pick up items or ask for more
  86. information in any of the rooms.  Try telling the system to look
  87. around the room and see what additional information it gives you
  88. for each room, some of the clues for solving the puzzle are given
  89. in the clues issued in response to a look command.  Another
  90. important command is inventory which will give you a list of the
  91. items you possess at any given point in time.  Type the word
  92. inventory at this time to see what items you possess.
  93.  
  94. The remainder of the commands consist of two words, a verb and a
  95. noun.  These can be given in either order, since the system is
  96. smart enough to know the difference, and additional words may be
  97. given following the legal words.  If you give the system a command
  98. that is not in its limited vocabulary, it will tell you it doesn't
  99. understand that word.  Try telling the system to drop an item you
  100. possess, or get an item that is located in the room you are
  101. currently in.
  102.  
  103. Several friends have played this game with no more knowledge than
  104. you have been given.  One solved it in 40 minutes, but most took
  105. about an hour to complete the game.  After you play the game for
  106. awhile, return to the text and we will study the source code for
  107. the game.  The entire source code for the game is on your
  108. distribution disk.  The game was purposely kept small so the code
  109. could be easily grasped by a programming student.  There is no
  110. reason the game could not have been made much larger by the
  111. addition of more rooms, items, and traps.  You may choose to do
  112. just that to gain experience in working with C++.
  113.  
  114.  
  115.  
  116.  
  117.                                                         Page 12-2
  118.  
  119.                               Chapter 12 - Flyaway Adventure Game
  120.  
  121.  
  122. A FEW SPECIAL CONSTANTS
  123. _________________________________________________________________
  124.  
  125. The file named FLYAWAY.H contains the             ===============
  126. definitions for TRUE and FALSE as well as the        FLYAWAY.H
  127. enumerated type defining the legal dictionary of  ===============
  128. words for use in playing the game.  The list was
  129. started at a value of 1 so the value of zero can
  130. be used to indicate that the word in question was not in the
  131. library and hence not a legal word for use with the game.
  132.  
  133. The #ifndef in line 5 is required because this header file is
  134. included in many of the other files and if it is included more than
  135. once, there will be a multiple definition, and hence an error.  A
  136. class only needs to be defined once, so after it is defined by one
  137. of the includes, the name ITEMS_H will be defined and any other
  138. defines will be ignored.  This is necessary because of the separate
  139. compilation capability of C++.  This was described in more detail
  140. near the end of chapter 7.
  141.  
  142.  
  143. THE FIRST CLASS - clock
  144. _________________________________________________________________
  145.  
  146. Examine the file named CLOCK.H for the            ===============
  147. definition of the clock class.  This is the           CLOCK.H
  148. class for the game clock, and only one instance   ===============
  149. of this class will be used.  It will be used for
  150. the object time_of_day defined in line 23 of
  151. FLYAWAY.CPP.
  152.  
  153. The class is very simple, consisting of only two variables, the
  154. hour and the minute, and four methods.  The first method is the
  155. constructor used to initialize the clock to 8:51 as you can see if
  156. you refer to the implementation of this class in the file named
  157. CLOCK.CPP.  The next two methods are used to get the current values
  158. of the two variables.  The final method is much more interesting
  159. since it does more.  It updates the time of day clock and outputs
  160. the user prompt to ask for the next command.  This may not be the
  161. best place to output the user prompt since this class is devoted
  162. to the time of day and associated operations, but this was chosen
  163. as the place to do it since the time of day is part of the user
  164. prompt.  You will notice that the clock was initialized to 8:51,
  165. but the first time output was 8:52 when you played the game.  In
  166. order to simplify the coding later, when we need to decide if we
  167. made it to the plane on time, the time was incremented at the
  168. beginning of each game move.  The time is therefore the same when
  169. the command is entered and when it is executed, hence the time is
  170. incremented prior to even the first output.
  171.  
  172. The clock class is by far the simplest class in the adventure game
  173. and should be simple for you to understand.  After you are sure you
  174. understand it, we will go on to the next class.
  175.  
  176.                                                         Page 12-3
  177.  
  178.                               Chapter 12 - Flyaway Adventure Game
  179.  
  180.  
  181. INPUT COMMAND PARSING
  182. _________________________________________________________________
  183.  
  184. The input command parsing routines are defined    ===============
  185. within the words class and the code for the           WORDS.H
  186. class is in WORDS.CPP.  The code is               ===============
  187. straightforward and simple to understand if you
  188. study it, so only a few comments will be made
  189. about this class.
  190.  
  191. The method get_command() reads two words from the keyboard by
  192. calling the function read_a_line() and stores the words in the
  193. class members verb and noun.  It stores zero for either or both of
  194. the words if it does not find a valid noun and a valid verb.
  195.  
  196. Two methods are included to provide the verb or noun which was
  197. input as the last user input.  This allows any code that has
  198. visibility of the object based on this class to find out what the
  199. player would like to do.
  200.  
  201. There are four methods beginning with is_ in this class that are
  202. used to determine if a word is a verb, a noun, a direction, or an
  203. operation.  These are called upon from various places within the
  204. program.  What they do should be easy for you to understand, but
  205. it will take a little thought on your part to see why these are
  206. needed in other parts of the code.
  207.  
  208. Finally the simple method named stop_game() is used to set the verb
  209. to the value of quit so the game will be ended by the control logic
  210. in the main program FLYAWAY.CPP.
  211.  
  212. All of the source code for the implementation is given in the file
  213. named WORDS.CPP.  Since this code is fairly simple and well
  214. commented, you will be left on your own to study it to whatever
  215. depth you desire.
  216.  
  217.  
  218. THE SECOND CLASS - items
  219. _________________________________________________________________
  220.  
  221. If you examine the files named ITEMS.H and        ===============
  222. ITEMS.CPP, you will find the complete                 ITEMS.H
  223. definitions of the handling of the items that     ===============
  224. you carried around the airport in the game.
  225. There were exactly four transportable items that
  226. could be located in each room or carried by yourself, the keys, the
  227. candy, the ticket, and the money.  The keys and the money keep you
  228. from getting through security and the ticket and candy are required
  229. to get you safely on the plane and enroute to your destination.
  230.  
  231. The four items are stored in the class named items in the form of
  232. TRUE or FALSE since that is the only required indication.  A TRUE
  233. means the item is located here, and a FALSE means the item is not
  234.  
  235.                                                         Page 12-4
  236.  
  237.                               Chapter 12 - Flyaway Adventure Game
  238.  
  239. here.  The values of TRUE and FALSE are defined in FLYAWAY.H.
  240. Finally, there are six methods to operate on these items.
  241.  
  242. The first method is a constructor to set all items to FALSE, and
  243. the next two are used to either get a specific item, or drop one.
  244. The fourth method is used to tell us if the item is located here
  245. and the last two are used to tell us what items are on hand in this
  246. location.  You will notice that the final two are different because
  247. different text was desired depending on whether you are carrying
  248. the items, or they are located in a room somewhere.
  249.  
  250. This file, like all other header files, is protected from multiple
  251. inclusion by the #ifndef construct discussed earlier.
  252.  
  253.  
  254. This class is used in line 24 of FLYAWAY.CPP to define an object
  255. for the player named personal_items which stores the list of items
  256. the player is carrying around.  It is also used in the class
  257. location as an embedded or nested object to store the items that
  258. are located in each of the 19 locations in the game.
  259.  
  260. Once again, the implementation for this class is so simple that you
  261. will have no difficulty in understanding it.
  262.  
  263.  
  264. THE FLIGHT AND GATE CLASS - schedule
  265. _________________________________________________________________
  266.  
  267. Examine the files named SCHEDULE.H and           ================
  268. SCHEDULE.CPP for our first example of a rather      SCHEDULE.H
  269. large class, the one that does the flight and    ================
  270. gate scheduling.  You will find a large number
  271. of variables in this class, and eight methods to
  272. handle the variables.  Instead of a detailed description of each
  273. variable and method, we will only give a brief overview of the
  274. class.
  275.  
  276. Only one object of this class is declared named flight_info in line
  277. 22 of the main program named FLYAWAY.CPP.  The constructor
  278. initializes the flight possibilities, and the method named
  279. shuffle_gates() shuffles all gates around if the player arrives at
  280. his correct gate without reading the monitor in the waiting area.
  281. Once the monitor in the waiting area is read, the flights_frozen
  282. variable is made TRUE.  Likewise, the players destination is
  283. changed during every move by the method named shuffle_flights()
  284. until the player reads his ticket invoking the method named
  285. list_actual_destination().
  286.  
  287. This class contains the methods to list the data seen on the
  288. monitor, as well as the data seen when invoking the command look
  289. at one of the gates.  Finally, this class contains the method named
  290. check_flight() which searches through the list of requirements to
  291. see if the player has completed all requirements to successfully
  292. reach the final destination for his vacation.
  293.  
  294.                                                         Page 12-5
  295.  
  296.                               Chapter 12 - Flyaway Adventure Game
  297.  
  298.  
  299. You will notice that several of the location objects were required
  300. to be available within this code and are listed as extern in lines
  301. 12 through 21 of the implementation of the class.  The only other
  302. thing to point out is the rest room requirement prior to boarding
  303. the flight.  Line 28 is where the global variable is defined and
  304. initialized, then in line 77 it is set TRUE if the current location
  305. is the rest room, since this is called once during each player
  306. move.  Finally, the state of this variable is checked in line 230
  307. of this file and the appropriate action taken.  You will note that
  308. the main program is not aware that the rest room variable exists
  309. or that anything happens as a result of this variable.  In addition
  310. to information hiding, we may coin a new term, something like
  311. "Information Ignorance", since the main program did not even need
  312. to be aware that there was a requirement to visit the rest room.
  313.  
  314. Even though this is a relatively large and complex class, it is
  315. well commented so no further information will be given concerning
  316. the implementation.
  317.  
  318.  
  319.  
  320. THE MOST USED CLASS - location
  321. _________________________________________________________________
  322.  
  323. The file named LOCATION.H is the header file for ================
  324. the class named location.  It is the class that     LOCATION.H
  325. controls all of the moves from location to       ================
  326. location.
  327.  
  328. This class is a bit unusual in that most of the stored data is in
  329. the form of pointers to the various entities.  The first four are
  330. the locations to which we will go if we move in one of the four
  331. directions from the current location.  You will note that they are
  332. pointers to those four locations.  Next we have pointers to two
  333. different character strings associated with this room.  Finally in
  334. line 22, we declare the object named list_of_items which is an
  335. object of class items defined earlier.  Note that this is an
  336. embedded class, a class embedded within the location class.  It is
  337. not a parent class which we are inheriting something from.  In fact
  338. we are instantiating an object of class items for use within the
  339. room since the room is allowed to store any combination of the four
  340. items contained in the class named items.
  341.  
  342. There is no constructor used with this class since we choose to
  343. initialize the locations one by one.  The method named init() has
  344. 6 variable parameters, all of which are pointers, associated with
  345. it which it uses to initialize the first six variables of this
  346. object.  The last variable, an object of class items, is
  347. initialized through use of the constructor associated with its
  348. class.  Referring to lines 40 through 171 of the implementation for
  349. the map class, you will find all of the initialization code for the
  350. 19 objects of class location.  If you drew a map when you played
  351. the game, you will see the interconnections between the various
  352.  
  353.                                                         Page 12-6
  354.  
  355.                               Chapter 12 - Flyaway Adventure Game
  356.  
  357. locations embedded in the initialization statements.  Notice there
  358. is no way to get back to the car from the passenger drop off area,
  359. because presumably the car leaves when you get out of it.
  360.  
  361. The next method, named move(), returns a pointer to the new
  362. location if a move was legal, otherwise it returns a NULL value.
  363. The observant student will also notice that there are special cases
  364. involved with getting out of the snack bar and getting through
  365. security.  These are located here because they are part of the move
  366. logic.  If you played the game to the complete conclusion, you
  367. surely had trouble with at least one of these situations.
  368.  
  369. The rest of the methods in this class should be self explanatory
  370. and will not be discussed any further.
  371.  
  372.  
  373.  
  374. THE LOCATION MESSAGES
  375. _________________________________________________________________
  376.  
  377. Examine the file named MESSAGE.TXT for a          ===============
  378. complete listing of the messages output to the      MESSAGE.TXT
  379. monitor when each location was entered.  You      ===============
  380. will also find the text for each of the messages
  381. output in response to a look command in this
  382. file.  These were put into a separate file only for the purpose of
  383. reducing the size of the map class implementation file.  It does
  384. not reduce the compile time since these messages are not separately
  385. compiled.  They are included into the file and compiled each time
  386. the map file MAP.CPP is compiled.  You will note that a few of the
  387. messages have no text at all, only the empty quote marks, but are
  388. included in order to have something for the initialization code to
  389. work with.
  390.  
  391. Three other messages are stored here for convenience in lines 5
  392. through 40.  Their use and meaning should be self-evident.
  393.  
  394.  
  395.  
  396. THE MAIN PROGRAM
  397. _________________________________________________________________
  398.  
  399. We finally reach the main program, the one that   ===============
  400. actually does the top level control.  Examine       FLYAWAY.CPP
  401. the program named FLYAWAY.CPP and we will look    ===============
  402. at some of its interesting characteristics.
  403.  
  404. Beginning with the main() entry point itself, we see that following
  405. a call to airport.initialize(), we enter a single do while loop
  406. which terminates when the player enters the word quit or when the
  407. verb quit comes up some other way.  There are other ways to set the
  408. verb to quit because it is generated internally in some cases such
  409. as at end of game.
  410.  
  411.  
  412.                                                         Page 12-7
  413.  
  414.                               Chapter 12 - Flyaway Adventure Game
  415.  
  416. The loop itself consists of 5 method calls.  First we call the
  417. function named input_words.get_command() to get the players input
  418. command in line 30.  Next we send two messages to the object named
  419. flight_info to shuffle the flights and gates if the proper actions
  420. have not been performed, then we call airport.perform_action()
  421. which we will describe in a few paragraphs.  Finally, we send a
  422. messages to the object named flight_info to check if the player has
  423. reached one of the gates.  Remember that within most of the methods
  424. we perform checks to see if we need to do the thing requested in
  425. the message, then either perform the action or simply return to the
  426. caller or message sender.
  427.  
  428.  
  429. THE WORKING METHOD
  430. _________________________________________________________________
  431.  
  432. The only function we have not mentioned yet is    ===============
  433. the one that does most of the interesting work,        MAP.H
  434. the function named perform_action() which begins  ===============
  435. in line 183 of the MAP.CPP file.  This function
  436. looks at the verb and noun, if there is one, and
  437. causes the correct action to be performed.  Because of the way we
  438. packaged all of the other routines, this function is a snap to
  439. implement and to study.  If you go through each of the else if
  440. clauses in this function, you will have no trouble understanding
  441. what action is taken for each of the input commands.  You will
  442. notice that many of the actions have conditional clauses before the
  443. action is taken.  For example, it is illegal to buy candy unless
  444. the player has money, the location has candy, and the location must
  445. be the snack_bar according to the rules of the game.
  446.  
  447. Finally, at the end of this method in line 277, we have the default
  448. case if nothing else was accomplished.  It is assumed that there
  449. was something funny requested such as a request to get a monitor.
  450. Both of these are legal words but they make no sense together.
  451.  
  452.  
  453.  
  454. FINAL COMMENTS ON FLYAWAY
  455. _________________________________________________________________
  456.  
  457. Now that you have played the game for awhile and studied the game
  458. in detail, you should have an appreciation for how this game can
  459. be written.  Of course, it could be written in any of several
  460. thousand different ways of packaging and definition.  This has been
  461. only one of the ways.
  462.  
  463. Because the student may be left with the sinking feeling that this
  464. method simply fell out of the sky or was arrived at in some other
  465. esoteric way, it would only be fair to point out that several
  466. earlier attempts at outlining this project were attempted and
  467. rejected prior to this arrangement.  Also, when this tutorial was
  468. being updated from version 2.0 to 2.2, the entire program was
  469. restructured.  In version 2.0 and prior versions, about 50% of the
  470.  
  471.                                                         Page 12-8
  472.  
  473.                               Chapter 12 - Flyaway Adventure Game
  474.  
  475. code was in classes, but due to additional programing experience,
  476. about 98% of the flyaway program is now encapsulated in classes.
  477.  
  478. Object oriented programming requires the same forethought as non-
  479. object oriented programming, but the object oriented compiler will
  480. help you in the coding and debugging phase since the compiler will
  481. find and flag many of the oversight errors we are so good at
  482. introducing into our code.  It was observed during the coding and
  483. debugging phase of this project that in nearly every case, when the
  484. program finally got through the compiler, the program would
  485. actually run without bombing out the system.  This is not always
  486. the case using any standard procedural programming language.
  487.  
  488.  
  489.  
  490.  
  491. YOUR PROGRAMMING PROJECT
  492. _________________________________________________________________
  493.  
  494. This programming assignment is intended to give you a little
  495. experience in working with a relatively large project as opposed
  496. to the very small programs we have been working with in this
  497. tutorial.
  498.  
  499. Add a suitcase to the game, to be found in the car at arrival, and
  500. which must be checked in at the ticket counter prior to attempting
  501. to get through airport security.  This will not be trivial since
  502. several classes will be affected.  Some of the operations you will
  503. have to do are listed below.
  504.  
  505. 1.   Add the noun "suitcase" and the verb "check" to the word list.
  506.      Of course, they must be entered at the right place in the
  507.      list.
  508.  
  509. 2.   Add the suitcase to the items class, including additional code
  510.      to each of its methods.
  511.  
  512. 3.   Initialize the items at location your_car to include the
  513.      suitcase.
  514.  
  515. 4.   Add an additional check when passing through security to check
  516.      that the player is not carrying the suitcase.  You can add any
  517.      sort of penalty desired, including death by firing squad for
  518.      attempting such an obviously crooked deed.
  519.  
  520. 5.   You will need to add a check when the player finally gets on
  521.      his correct airplane to see that he checked his suitcase.  If
  522.      he did not, you could output any desired text indicating
  523.      stupidity or forgetfulness.
  524.  
  525. Since I have not actually added the suitcase to the game and tested
  526. it, I am not sure that this is all that will be required, but it
  527. should be the majority of effort required.  The bottom line of this
  528. effort is that if you understand this program enough to perform
  529.  
  530.                                                         Page 12-9
  531.  
  532.                               Chapter 12 - Flyaway Adventure Game
  533.  
  534. this modification, you have a good understanding of how the program
  535. works and how objects work together to perform a task.
  536.  
  537. Once you understand this program, you should define a programming
  538. project for yourself that will use object oriented programming
  539. techniques and begin designing and programming it.  The best way
  540. to learn to use OOP is to actually use it.
  541.  
  542. Good luck in your OOP endeavors.
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                                        Page 12-10